package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.bo.ModulesAdvertiseBO;
import com.junzhiit.nengji.bo.ModulesBO;
import com.junzhiit.nengji.bo.ModulesGoodsBO;
import com.junzhiit.nengji.dao.ColumnDAO;
import com.junzhiit.nengji.dao.ModulesAdvertiseDAO;
import com.junzhiit.nengji.dao.ModulesDAO;
import com.junzhiit.nengji.dao.ModulesGoodsDAO;
import com.junzhiit.nengji.model.Column;
import com.junzhiit.nengji.model.Modules;
import com.junzhiit.nengji.model.enums.GoodsHistoryType;
import com.junzhiit.nengji.shop.service.ColumnService;
import com.junzhiit.nengji.shop.service.GoodsHistoryService;
import com.junzhiit.nengji.shop.web.exception.ColumnNotFoundException;
import com.junzhiit.nengji.shop.web.vo.GoodsHistoryAEVO;
import com.junzhiit.nengji.shop.web.vo.ListVO;
import com.junzhiit.nengji.shop.web.vo.ModulesAdvertiseVO;
import com.junzhiit.nengji.shop.web.vo.ModulesGoodsVO;
import com.junzhiit.nengji.shop.web.vo.ModulesVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

@Service
public class ColumnServiceImpl extends BaseServiceImpl implements ColumnService {

	@Autowired
	private ModulesDAO modulesDAO;

	@Autowired
	private ColumnDAO columnDAO;

	@Autowired
	private ModulesGoodsDAO modulesGoodsDAO;

	@Autowired
	private ModulesAdvertiseDAO modulesAdvertiseDAO;

	@Autowired
	private GoodsHistoryService goodsHistoryService;

	@Override
	@Transactional
	public ResultVO findColumnInfo(String userId, String columnCode, String cityCode, String shopId) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.getColumnByCode(columnCode, shopId);
		if (column == null) {
			throw new ColumnNotFoundException("栏目没找到");
		}

		// 查询栏目的所有模块
		List<ModulesBO> modulesBOList = modulesDAO.findModules(columnCode, shopId);
		// 查询栏目的所有模块的所有模块广告
		List<ModulesAdvertiseBO> modulesAdvertiseBOList = modulesAdvertiseDAO.findModulesAdvertise(columnCode, shopId);
		// 查询栏目的所有模块的所有模块商品
		List<ModulesGoodsBO> modulesGoodsBOList = modulesGoodsDAO.findModulesGoodsBelongToShop(columnCode, cityCode, shopId);

		List<ModulesVO> list = new ArrayList<ModulesVO>();
		List<GoodsHistoryAEVO> goodsHistoryAEVOList = new ArrayList<GoodsHistoryAEVO>();
		for (ModulesBO modulesBO : modulesBOList) {
			ModulesVO modulesVO = new ModulesVO();
			BeanUtils.copyProperties(modulesBO, modulesVO);

			// 拼装模块广告
			List<ModulesAdvertiseVO> modulesAdvertiseVOList = new ArrayList<ModulesAdvertiseVO>();
			for (ModulesAdvertiseBO modulesAdvertiseBO : modulesAdvertiseBOList) {
				if (modulesBO.getId().equals(modulesAdvertiseBO.getModulesId())) {
					ModulesAdvertiseVO modulesAdvertiseVO = new ModulesAdvertiseVO();
					BeanUtils.copyProperties(modulesAdvertiseBO, modulesAdvertiseVO);
					modulesAdvertiseVOList.add(modulesAdvertiseVO);
				}
			}
			if (modulesAdvertiseVOList.size() > 0) {
				modulesVO.setModulesAdvertiseList(modulesAdvertiseVOList);
			}

			// 拼装模块商品
			List<ModulesGoodsVO> modulesGoodsVOList = new ArrayList<ModulesGoodsVO>();
			
			for (int i = 0; i < modulesGoodsBOList.size(); i++) {
				
				if(modulesGoodsVOList.size()>=modulesBO.getGoodsExhibitionQuantity()){
					break;
				}
				
				ModulesGoodsBO modulesGoodsBO = modulesGoodsBOList.get(i);
				if (modulesBO.getId().equals(modulesGoodsBO.getModulesId())) {
					ModulesGoodsVO modulesGoodsVO = new ModulesGoodsVO();
					BeanUtils.copyProperties(modulesGoodsBO, modulesGoodsVO);
					
					modulesGoodsVOList.add(modulesGoodsVO);

					GoodsHistoryAEVO goodsHistoryAEVO = new GoodsHistoryAEVO();
					goodsHistoryAEVO.setKey(
							userId + "-" + modulesGoodsBO.getGoodsId() + "-" + GoodsHistoryType.EXPOSURE.toIntValue());
					goodsHistoryAEVO.setGoodsId(modulesGoodsBO.getGoodsId());
					goodsHistoryAEVO.setShopId(modulesGoodsBO.getShopId());
					goodsHistoryAEVO.setUserId(userId);
					goodsHistoryAEVO.setNumber(1);
					goodsHistoryAEVO.setCreatedTime(new Date());
					goodsHistoryAEVO.setType(GoodsHistoryType.EXPOSURE.toIntValue());
					goodsHistoryAEVOList.add(goodsHistoryAEVO);
				}
			}
			if (modulesGoodsVOList.size() > 0) {
				modulesVO.setModulesGoodsList(modulesGoodsVOList);
			}

			list.add(modulesVO);
		}

		if (StringUtils.isNotBlank(userId)) {
			goodsHistoryService.add(goodsHistoryAEVOList);
		}

		result.setData(list);
		return result;
	}

	@Override
	@Transactional
	public ResultVO findModulesInfo(String userId, String modulesCode, String cityCode, String shopId,Integer pageNumber,Integer maxPageRow) {
		ResultVO result = new ResultVO();
		Modules modules = modulesDAO.getModulesByCode(modulesCode, shopId);

		if (modules == null) {
			throw new ColumnNotFoundException("模块没找到");
		}

		// 查询栏目的所有模块的所有模块广告
		List<ModulesAdvertiseBO> modulesAdvertiseBOList = modulesAdvertiseDAO.findModulesAdvertiseByModulesCode(modulesCode, shopId);
		// 查询栏目的所有模块的所有模块商品
		List<ModulesGoodsBO> modulesGoodsBOList = modulesGoodsDAO.findModulesGoodsByModulesCode(modulesCode, cityCode, shopId,pageNumber,maxPageRow);

		List<ModulesVO> list = new ArrayList<ModulesVO>();
		List<GoodsHistoryAEVO> goodsHistoryAEVOList = new ArrayList<GoodsHistoryAEVO>();
		ModulesVO modulesVO = new ModulesVO();
		BeanUtils.copyProperties(modules, modulesVO);

		// 拼装模块广告
		List<ModulesAdvertiseVO> modulesAdvertiseVOList = new ArrayList<ModulesAdvertiseVO>();
		for (int i = 0; i < modulesAdvertiseBOList.size(); i++) {
			ModulesAdvertiseBO modulesAdvertiseBO = modulesAdvertiseBOList.get(i);
			if (modules.getId().equals(modulesAdvertiseBO.getModulesId())) {
				ModulesAdvertiseVO modulesAdvertiseVO = new ModulesAdvertiseVO();
				BeanUtils.copyProperties(modulesAdvertiseBO, modulesAdvertiseVO);
				modulesAdvertiseVOList.add(modulesAdvertiseVO);
			}
		}
		if (modulesAdvertiseVOList.size() > 0) {
			modulesVO.setModulesAdvertiseList(modulesAdvertiseVOList);
		}

		// 拼装模块商品
		List<ModulesGoodsVO> modulesGoodsVOList = new ArrayList<ModulesGoodsVO>();
		for (int i = 0; i < modulesGoodsBOList.size(); i++) {
			ModulesGoodsBO modulesGoodsBO = modulesGoodsBOList.get(i);
			if (modules.getId().equals(modulesGoodsBO.getModulesId())) {
				ModulesGoodsVO modulesGoodsVO = new ModulesGoodsVO();
				BeanUtils.copyProperties(modulesGoodsBO, modulesGoodsVO);
				modulesGoodsVOList.add(modulesGoodsVO);

				GoodsHistoryAEVO goodsHistoryAEVO = new GoodsHistoryAEVO();
				goodsHistoryAEVO.setKey(
						userId + "-" + modulesGoodsBO.getGoodsId() + "-" + GoodsHistoryType.EXPOSURE.toIntValue());
				goodsHistoryAEVO.setGoodsId(modulesGoodsBO.getGoodsId());
				goodsHistoryAEVO.setShopId(modulesGoodsBO.getShopId());
				goodsHistoryAEVO.setUserId(userId);
				goodsHistoryAEVO.setNumber(1);
				goodsHistoryAEVO.setCreatedTime(new Date());
				goodsHistoryAEVO.setType(GoodsHistoryType.EXPOSURE.toIntValue());
				goodsHistoryAEVOList.add(goodsHistoryAEVO);
			}
		}
		if (modulesGoodsVOList.size() > 0) {
			modulesVO.setModulesGoodsList(modulesGoodsVOList);
		}

		list.add(modulesVO);

		if (StringUtils.isNotBlank(userId)) {
			goodsHistoryService.add(goodsHistoryAEVOList);
		}

		result.setData(list);
		return result;
	}

	@Override
	public ResultVO home(String columnCode, String shopId) {
		ResultVO result = new ResultVO();
		Column column = columnDAO.getColumnByCode(columnCode, shopId);
		if (column == null) {
			throw new ColumnNotFoundException("栏目没找到");
		}

		// 查询栏目的所有模块的所有模块广告
		List<ModulesAdvertiseBO> modulesAdvertiseBOList = modulesAdvertiseDAO.findModulesAdvertise(columnCode, shopId);

		Map<String, ModulesVO> modulesVOMap = new LinkedHashMap<String, ModulesVO>();

		for (ModulesAdvertiseBO bo : modulesAdvertiseBOList) {
			ModulesVO modulesVO = modulesVOMap.get(bo.getModulesCode());
			if (modulesVO == null) {
				modulesVO = new ModulesVO();
				BeanUtils.copyProperties(bo, modulesVO);
				modulesVO.setId(bo.getModulesId());
				modulesVO.setCode(bo.getModulesCode());
				modulesVO.setName(bo.getModulesName());
				List<ModulesAdvertiseVO> modulesAdvertiseVOList = new ArrayList<ModulesAdvertiseVO>();
				modulesVO.setModulesAdvertiseList(modulesAdvertiseVOList);
				modulesVOMap.put(modulesVO.getCode(), modulesVO);
			}

			ModulesAdvertiseVO modulesAdvertiseVO = new ModulesAdvertiseVO();
			BeanUtils.copyProperties(bo, modulesAdvertiseVO);
			modulesVO.getModulesAdvertiseList().add(modulesAdvertiseVO);
		}
		Collection<ModulesVO> data = modulesVOMap.values();
		result.setData(data);
		return result;
	}

	@Override
	public ListVO homeGoods(String userId, String columnCode, String modulesCode, String cityCode, String shopId,
			Integer pageNumber, Integer maxPageRow) {
		Column column = columnDAO.getColumnByCode(columnCode, shopId);
		if (column == null) {
			throw new ColumnNotFoundException("栏目没找到");
		}

		// 查询栏目的所有模块的所有模块商品
		List<ModulesGoodsBO> modulesGoodsBOList = modulesGoodsDAO.findModulesGoods(columnCode, cityCode, modulesCode,
				pageNumber, maxPageRow);

		Map<String, ModulesVO> modulesVOMap = new LinkedHashMap<String, ModulesVO>();
		List<GoodsHistoryAEVO> goodsHistoryAEVOList = new ArrayList<GoodsHistoryAEVO>();
		for (ModulesGoodsBO bo : modulesGoodsBOList) {
			ModulesVO modulesVO = modulesVOMap.get(bo.getModulesCode());
			if (modulesVO == null) {
				modulesVO = new ModulesVO();
				BeanUtils.copyProperties(bo, modulesVO);
				modulesVO.setId(bo.getModulesId());
				modulesVO.setCode(bo.getModulesCode());
				modulesVO.setName(bo.getModulesName());
				List<ModulesGoodsVO> modulesGoodsVOList = new ArrayList<ModulesGoodsVO>();
				modulesVO.setModulesGoodsList(modulesGoodsVOList);
				modulesVOMap.put(modulesVO.getCode(), modulesVO);
			}

			ModulesGoodsVO modulesGoodsVO = new ModulesGoodsVO();
			BeanUtils.copyProperties(bo, modulesGoodsVO);
			modulesVO.getModulesGoodsList().add(modulesGoodsVO);

			GoodsHistoryAEVO goodsHistoryAEVO = new GoodsHistoryAEVO();
			goodsHistoryAEVO.setKey(userId + "-" + bo.getGoodsId() + "-" + GoodsHistoryType.EXPOSURE.toIntValue());
			goodsHistoryAEVO.setGoodsId(bo.getGoodsId());
			goodsHistoryAEVO.setShopId(bo.getShopId());
			goodsHistoryAEVO.setUserId(userId);
			goodsHistoryAEVO.setNumber(1);
			goodsHistoryAEVO.setCreatedTime(new Date());
			goodsHistoryAEVO.setType(GoodsHistoryType.EXPOSURE.toIntValue());
			goodsHistoryAEVOList.add(goodsHistoryAEVO);
		}

		if (StringUtils.isNotBlank(userId)) {
			goodsHistoryService.add(goodsHistoryAEVOList);
		}

		Integer totalCount = modulesGoodsDAO.getTotalCount(columnCode, cityCode, modulesCode);

		ListVO listVO = new ListVO();
		listVO.setIndex(pageNumber);
		listVO.setCount(totalCount);
		listVO.setList(modulesVOMap.get(modulesCode));

		return listVO;
	}

	@Override
	public ListVO columnInfo(String userId, String columnCode, String cityCode, String shopId, Integer pageNumber,
			Integer maxPageRow) {
		Column column = columnDAO.getColumnByCode(columnCode, null);
		if (column == null) {
			throw new ColumnNotFoundException("栏目没找到");
		}

		// 查询栏目的所有模块
		List<ModulesBO> modulesBOList = modulesDAO.findModules(columnCode, shopId);
		// 查询栏目的所有模块的所有模块广告
		List<ModulesAdvertiseBO> modulesAdvertiseBOList = modulesAdvertiseDAO.findModulesAdvertise(columnCode, shopId);
		// 查询栏目的所有模块的所有模块商品
		List<ModulesGoodsBO> modulesGoodsBOList = modulesGoodsDAO.findModulesGoods(columnCode, cityCode, null,
				pageNumber, maxPageRow);

		List<ModulesVO> list = new ArrayList<ModulesVO>();
		List<GoodsHistoryAEVO> goodsHistoryAEVOList = new ArrayList<GoodsHistoryAEVO>();
		for (ModulesBO modulesBO : modulesBOList) {
			ModulesVO modulesVO = new ModulesVO();
			BeanUtils.copyProperties(modulesBO, modulesVO);

			// 拼装模块广告
			List<ModulesAdvertiseVO> modulesAdvertiseVOList = new ArrayList<ModulesAdvertiseVO>();
			for (ModulesAdvertiseBO modulesAdvertiseBO : modulesAdvertiseBOList) {
				if (modulesBO.getId().equals(modulesAdvertiseBO.getModulesId())) {
					ModulesAdvertiseVO modulesAdvertiseVO = new ModulesAdvertiseVO();
					BeanUtils.copyProperties(modulesAdvertiseBO, modulesAdvertiseVO);
					modulesAdvertiseVOList.add(modulesAdvertiseVO);
				}
			}
			if (modulesAdvertiseVOList.size() > 0) {
				modulesVO.setModulesAdvertiseList(modulesAdvertiseVOList);
			}

			// 拼装模块商品
			List<ModulesGoodsVO> modulesGoodsVOList = new ArrayList<ModulesGoodsVO>();
			for (ModulesGoodsBO modulesGoodsBO : modulesGoodsBOList) {
				if (modulesBO.getId().equals(modulesGoodsBO.getModulesId())) {
					ModulesGoodsVO modulesGoodsVO = new ModulesGoodsVO();
					BeanUtils.copyProperties(modulesGoodsBO, modulesGoodsVO);
					modulesGoodsVOList.add(modulesGoodsVO);

					GoodsHistoryAEVO goodsHistoryAEVO = new GoodsHistoryAEVO();
					goodsHistoryAEVO.setKey(
							userId + "-" + modulesGoodsBO.getGoodsId() + "-" + GoodsHistoryType.EXPOSURE.toIntValue());
					goodsHistoryAEVO.setGoodsId(modulesGoodsBO.getGoodsId());
					goodsHistoryAEVO.setShopId(modulesGoodsBO.getShopId());
					goodsHistoryAEVO.setUserId(userId);
					goodsHistoryAEVO.setNumber(1);
					goodsHistoryAEVO.setCreatedTime(new Date());
					goodsHistoryAEVO.setType(GoodsHistoryType.EXPOSURE.toIntValue());
					goodsHistoryAEVOList.add(goodsHistoryAEVO);
				}
			}
			if (modulesGoodsVOList.size() > 0) {
				modulesVO.setModulesGoodsList(modulesGoodsVOList);
			}

			list.add(modulesVO);
		}

		if (StringUtils.isNotBlank(userId)) {
			goodsHistoryService.add(goodsHistoryAEVOList);
		}

		Integer totalCount = modulesGoodsDAO.getTotalCount(columnCode, cityCode, null);

		ListVO listVO = new ListVO();
		listVO.setIndex(pageNumber);
		listVO.setCount(totalCount);
		listVO.setList(list);

		return listVO;
	}

}
